package 第三章作业;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Work7 {
    static int count1 = 0;
    static int count2 = 0;
    static int count3 = 0;

    public static Map<String, Integer> getfile(File file) throws IOException {

        Map<String, Integer> map = new HashMap<>();

        if (file != null && file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        getfile(files[i]);
                    } else {
                        String[] str = new String[files.length];
                        String str1 = files[i].getName();
                        int index = str1.lastIndexOf(".");
                        if (str1.substring(index + 1, str1.length()).equals("java")) {
                            count1++;
                        }
                        if (str1.substring(index + 1, str1.length()).equals("doc")) {
                            count2++;
                        }
                        if (str1.substring(index + 1, str1.length()).equals("txt")) {
                            count3++;
                        }
                    }
                }
            }
        }
        map.put("java", count1);
        map.put("doc", count2);
        map.put("txt", count3);
        return map;
    }

    public static void main(String[] args) throws IOException {

        Map<String, Integer> map = Work7.getfile(new File("D:\\第七题"));
        System.out.println(map.entrySet());

    }
}
